/**
 * Logging panel by Jared Brayshaw
 * 
 * Created for Task # on FogBugz:
 * https://msoe.fogbugz.com/default.asp?###
 */
package hydrap2p.gui;

import java.util.List;
import java.util.Vector;

import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTable;
import javax.swing.table.TableColumn;

/**
 * A panel that displays various recorded information, such as download speed history or error messages.
 * 
 * @author Jared
 */
public class LoggingPanel extends JSplitPane
{	
	//the table that displays all the event logs
	private JTable logs;
	
	//the two parts of this panel
	public static final JPanel graphArea = new GraphPanel();
	private static final JScrollPane loggingArea = new JScrollPane();
	
	
	//singleton stuff
	private static volatile LoggingPanel instance;
	
	public static synchronized LoggingPanel getInstance(){
		if(instance == null){
			synchronized (LoggingPanel.class){
				if(instance == null){
					instance = new LoggingPanel();
				}
			}
		}
		return instance;
	}
	//end of singleton stuff
	
	
	/**
	 * Creates the panel by putting the speed history graph on the top half of a splitpane and
	 * the event logs table on the bottom half.
	 */
	private LoggingPanel(){
		super(JSplitPane.VERTICAL_SPLIT, graphArea, loggingArea);
		
		setOneTouchExpandable(true);
		setDividerLocation(300);		//there's probably a better way to do this, like getting the dimensions of the parent component
		setDividerSize(3);
		
		refreshDetails();
	}
	
	/**
	 * Makes sure that the panel displays the most recent information 
	 */
	public void refreshDetails()
	{
		//tell the graph to draw itself
		graphArea.repaint();
		
		//set up the table contents
		Vector<String> columnNames = new Vector<String>();
			columnNames.add("Message");
			columnNames.add("Time");
		Vector<Vector<String>> data = new Vector<Vector<String>>();

		List<MockLog> logList = GUIDataHandler.getInstance().getLogs();
		
		//add the logs
		for (int i = logList.size() - 1; i >= 0; i--)
		{
			MockLog ml = logList.get(i);
			Vector<String> v = new Vector<String>();
			v.add(ml.getMessage());
			v.add(ml.getTime());
			data.add(v);
		}
		
		logs = new JTable(data, columnNames) { public boolean isCellEditable(int rowIndex, int vColIndex) { return false; }};
		
		//set up the columns
			//figure out how to set an initial position for the column divider (preferably without getting width info from the panel)
		TableColumn column;
        
		column = logs.getColumnModel().getColumn(0);
        column.setMinWidth(75);
        
		column = logs.getColumnModel().getColumn(1);
        column.setMinWidth(125);
        column.setWidth(125);
		
		loggingArea.setViewportView(logs);
	}
}
